package com.shop.cereshop.app.service.product.impl;

import com.shop.cereshop.app.dao.product.CereProductStatsByDayDAO;
import com.shop.cereshop.app.param.product.ProductDTO;
import com.shop.cereshop.app.service.business.CereBusinessBuyerUserService;
import com.shop.cereshop.app.service.collect.CereBuyerFootprintService;
import com.shop.cereshop.app.service.product.CereProductVisitService;
import com.shop.cereshop.app.service.shop.CereShopConversionService;
import com.shop.cereshop.app.service.shop.CereShopVisitService;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.constant.ParamEnum;
import com.shop.cereshop.commons.domain.business.CereBusinessBuyerUser;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.collect.CereBuyerFootprint;
import com.shop.cereshop.commons.domain.product.CereProductStatsByDay;
import com.shop.cereshop.commons.domain.product.CereProductVisit;
import com.shop.cereshop.commons.domain.shop.CereShopConversion;
import com.shop.cereshop.commons.domain.shop.CereShopVisit;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.TimeUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class ProductAsyncService {

    private final CereShopVisitService cereShopVisitService;
    private final CereProductStatsByDayDAO cereProductStatsByDayDAO;
    private final CereBuyerFootprintService cereBuyerFootprintService;
    private final CereShopConversionService cereShopConversionService;
    private final CereBusinessBuyerUserService cereBusinessBuyerUserService;
    private final CereProductVisitService cereProductVisitService;

    @Async
    @Transactional(rollbackFor = Exception.class)
    public void addProductVisit(ProductDTO param, Long buyerUserId) throws CoBusinessException {
        String now = TimeUtils.yyMMddHHmmss();
        // 新增店铺访问记录
        CereShopVisit visit = new CereShopVisit();
        visit.setBuyerUserId(buyerUserId)
                .setShopId(param.getShopId())
                .setVisitTime(now)
                .setTerminal(param.getTerminal())
                .setSystem(param.getSystem())
                .setCity(param.getCityCode());
        cereShopVisitService.insert(visit);

        // 新增商品访问记录
        CereProductVisit productVisit = new CereProductVisit()
                .setProductId(param.getProductId())
                .setBuyerUserId(buyerUserId)
                .setTerminal(param.getTerminal())
                .setSystem(param.getSystem())
                .setCity(param.getCityCode());
        cereProductVisitService.saveProductVisit(productVisit);

        // 新增转化数据
        CereShopConversion cereShopConversion = new CereShopConversion();
        cereShopConversion.setShopId(param.getShopId())
                .setCreateTime(now)
                .setType(ParamEnum.CONVERSION_VISIT.getCode());
        cereShopConversionService.insert(cereShopConversion);
        // 新增足迹
        CereBuyerFootprint cereBuyerFootprint = new CereBuyerFootprint();
        cereBuyerFootprint.setBuyerUserId(buyerUserId)
                .setProductId(param.getProductId())
                .setCreateTime(now)
                .setShopId(param.getShopId())
                .setSkuId(param.getSkuId())
                .setSelected(IntegerEnum.NO.getCode());
        cereBuyerFootprintService.insert(cereBuyerFootprint);
        // 新增商家和客户的绑定关系
        CereBusinessBuyerUser bbu = new CereBusinessBuyerUser();
        bbu.setShopId(param.getShopId());
        bbu.setBuyerUserId(buyerUserId);
        cereBusinessBuyerUserService.addBusinessBuyerUser(bbu);
    }
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void addProductStats(ProductDTO param) {
        CereProductStatsByDay stats = new CereProductStatsByDay();
        stats.setShopId(param.getShopId());
        stats.setCreateDate(TimeUtils.today());
        stats.setProductId(param.getProductId());
        stats.setAddCartCount(0);
        stats.setVisitCount(1);
        stats.setSalesVolume(0);
        cereProductStatsByDayDAO.insertOrUpdate(stats);
    }
}
